package com.easybuilder.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;

@Slf4j
@Component
public class PhantomUtil {

    public static String render(String url) {

        try {
            Process exec = Runtime.getRuntime().exec("phantomjs spider.js " + url);
            BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(new String(line.getBytes(), "utf-8"));
            }
            // log.info("phantomjs 渲染后结果:");
            // log.info(sb.toString());
            return sb.toString();
        } catch (Exception e) {
            log.error("phantomjs 渲染失败，失败原因：{}" , e.getMessage());
        }
        return null;
    }
}
